﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using seccCableSeleccionado;

/// <summary>
/// Descripción breve de CConsultaSeccCableSeleccionado
/// </summary>

namespace consultasSql
{
    public class CConsultaSeccCableSeleccionado
    {
        
        /*UPDATE*/
        public bool updateSecc(List<CSeccCableSeleccionado> lista)
        {
            try
            {
                foreach (CSeccCableSeleccionado seccCable in lista)
                {
                    actualizarCampos(seccCable.Id, seccCable.IdOtCab);
                }
                return true;
            }
            catch { }

            return false;

        }

        private bool actualizarCampos(int  Id, string IdOtCab)
        {
            bool bandera = false;

            string consulta = "UPDATE SeccCable SET IdOtCab = @IdOtCab WHERE Id = @id";


            using (SqlConnection objConexion = new SqlConnection(CCadenaConexionSql.cadConexion()))
            {
                using (SqlCommand command = new SqlCommand())
                {
                    if (objConexion.State == ConnectionState.Closed)
                    { objConexion.Open(); }

                    command.CommandType = CommandType.Text;
                    command.CommandText = consulta;

                    command.Parameters.Clear();



                    command.Parameters.AddWithValue("@id", Id);

                    if (!String.IsNullOrEmpty(IdOtCab))
                    {
                        command.Parameters.Add(new SqlParameter("@IdOtCab", SqlDbType.Int));
                        command.Parameters["@IdOtCab"].Value = Convert.ToInt32(IdOtCab);
                    }
                    else
                    {
                        command.Parameters.AddWithValue("@IdOtCab", DBNull.Value);
                    }


                    command.Connection = objConexion;

                    try
                    {
                        command.ExecuteNonQuery();
                        bandera = true;
                    }
                    catch
                    {
                        bandera = false;
                    }

                    if (objConexion.State == ConnectionState.Open)
                    { objConexion.Close(); }
                }
            }
            return bandera;
        }


    }
}